/**
 *   eHOT. Desktop Java application allows to create notification messages,
 *   reminders, schedule your tasks and simply manage your time.
 *   Copyright (C) 2011  Anastasia Kolesnikova, Artem Perepelitsa
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package ehot.ui;

import java.util.InputMismatchException;
import java.util.Scanner;

import org.apache.log4j.Logger;

import ehot.persistence.INote;
import ehot.persistence.NotesContainer;

public class TextMenu {

	private static Logger logger = Logger.getLogger(TextMenu.class);

	private NotesContainer noteList = NotesContainer.getInstance();

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		logger.trace("Application started");
		try {
			TextMenu menu = new TextMenu();
			int retCode = 0;
			do {
				retCode = menu.getSelectedMenuItem();
			} while (retCode != 1);
		} catch (Exception e) {
			logger.fatal(e.getMessage(), e);
		} finally {
			logger.trace("Application terminated");
		}
	}

	private int getSelectedMenuItem() {
		int retCode = 0;
		Scanner in = new Scanner(System.in);

		this.show();
		try {
			int menuItem = in.nextInt();
			switch (menuItem) {
			case 0:
				in.close();
				noteList.save();
				retCode = 1;
				break;
			case 1:
				this.addNote();
				break;
			case 2:
				this.modifyNote();
				break;
			case 3:
				this.showAllNotes();
				break;
			case 4:
				this.showOneNote();
				break;
			case 5:
				this.removeNote();
				break;
			default:
				System.out
						.println("Wrong selection! Please, enter a valid menu item number! ");
			}
		}

		catch (InputMismatchException exception) {
			logger.error("wrong input");
			System.out
					.println("Wrong selection! Please, enter a valid menu item number! ");
		}
		return retCode;
	}

	private void show() {
		System.out.println("=== eHOT main Menu ===");
		System.out.println("1. Add note");
		System.out.println("2. Modify note");
		System.out.println("3. Show all notes");
		System.out.println("4. Show note by number");
		System.out.println("5. Remove note by number");
		System.out.println("0 - quit eHOT");
		System.out.println("\nPlease, enter the menu item number: ");
	}

	private void addNote() {
		System.out.println("eHot - Add note (0 - to return to the main menu)");
		System.out.println("------------------------------------------------");
		System.out.println("Enter the note text to add:");

		Scanner in = new Scanner(System.in);
		String noteString = "";
		noteString = in.nextLine();
		noteList.addNote(noteString);
	}

	private void modifyNote() {
		System.out.println("eHot - Modify note");
		System.out
				.println("---------------------------------------------------");
		System.out
				.println("-------------- Notes ------------------------------");
		this.showNotesList();
		System.out
				.println("---------------------------------------------------");
		System.out
				.println("Enter the note number to modify (0 - to return to the main menu):");

		Scanner in = new Scanner(System.in);
		Long noteNumber = in.nextLong();

		while (noteNumber != 0) {
			in.nextLine();
			if (noteList.containsNote(noteNumber)) {
				System.out.println("Please, enter new note text: ");
				String newText = in.nextLine();
				noteList.setNoteText(noteNumber,newText);
				System.out.println("Note was modified");
			} else {
				System.out.println("No such note!");
			}
			System.out
					.println("Enter the note number to modify (0 - to return to the main menu):");
			noteNumber = in.nextLong();
		}

	}

	private void removeNote() {
		System.out.println("eHot - Remove note");
		System.out
				.println("---------------------------------------------------");
		System.out
				.println("-------------- Notes ------------------------------");
		this.showNotesList();
		System.out
				.println("---------------------------------------------------");
		System.out
				.println("Enter the note number to remove (0 - to return to the main menu):");

		Scanner in = new Scanner(System.in);
		Long noteNumber = in.nextLong();
		while (noteNumber != 0) {
			if (noteList.removeNote(noteNumber)) {
				System.out.println("Note was removed");
			} else {
				System.out.println("No such note!");
			}
			System.out
					.println("Enter the note number to remove (0 - to return to the main menu):");
			noteNumber = in.nextLong();
		}
	}

	private void showAllNotes() {
		System.out
				.println("eHot - Notes list (0 - to return to the main menu)");
		System.out
				.println("--------------------------------------------------");

		this.showNotesList();
		Scanner in = new Scanner(System.in);
		try {
			for (int menuItem = 1; menuItem != 0; menuItem = in.nextInt())
				;
		} catch (InputMismatchException exception) {
		}
	}

	private void showOneNote() {
		System.out.println("eHot - Show one note");
		System.out
				.println("--------------------------------------------------");
		System.out
				.println("Enter the note number to display (0 - to return to the main menu):");
		Scanner in = new Scanner(System.in);
		Long noteNumber = in.nextLong();

		while (noteNumber != 0) {
			if (noteList.containsNote(noteNumber)) {
				System.out.println("\tNote #\t\tNote text");
				StringBuilder stringToPrint = new StringBuilder("\t");
				stringToPrint.append(noteNumber)
						.append("\t\t")
						.append(noteList.getNoteText(noteNumber));
				System.out.println(stringToPrint.toString());
			} else {
				System.out.println("No such note!");
			}
			System.out
					.println("Enter the note number to display (0 - to return to the main menu):");
			noteNumber = in.nextLong();
		}

	}

	private void showNotesList() {
		System.out.println("\tNote #\t\tNote text");

		for (INote currentNote : noteList.values()) {
			StringBuilder stringToPrint = new StringBuilder("\t");
			stringToPrint.append(currentNote.getId()+1).append("\t\t")
					.append(currentNote.getDescription());
			System.out.println(stringToPrint.toString());
		}

	}

}
